package com.ss.android.download;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.design.a;
import android.util.Pair;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.browser.R;
import com.ss.android.common.download.DownloadShortInfo;
import com.ss.android.common.download.DownloadInfoChangeListener;
import com.ss.android.common.download.IDownloadAdapter;

import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;

public class DownloadAdapter implements IDownloadAdapter {
    public DownloadAdapter() {

    }

    public long addDownloadTask(String arg8, String arg9, boolean arg10, Context arg11, String arg12, Map arg13, boolean arg14, boolean arg15, boolean arg16) {
        String v0_5 = null;
        com.ss.android.download.e.e$c v4;
        String v2 = null;
        Uri v0_3 = null;
        long v0_1;
        e v3;
        try {
            v3 = e.a(arg11);
            if (v3 != null) {
                try {
                    label_7:
                    v0_3 = Uri.parse(arg8);
                    v2 = v0_3.getLastPathSegment();
                    if (a.isTextEmpty(v2)) {
                        if (!a.isTextEmpty(arg9)) {
                            v2 = arg9;
                        } else {
                            if (arg10) {
                                v2 = "default.apk";
                            } else {
                                return -1;
                            }
                        }
                    }
                } catch (Throwable v0_2) {

                }
                if (a.isTextEmpty(arg9)) {
                    arg9 = v2;
                }
                v4 = new com.ss.android.download.e.e$c(v0_3);
                if (arg13 != null) {
                    Iterator v5 = arg13.entrySet().iterator();
                    while (v5.hasNext()) {
                        Object v0_4 = v5.next();
                        Object v1 = ((Map.Entry) v0_4).getKey();
                        v0_4 = ((Map.Entry) v0_4).getValue();
                        if (v1 == null) {
                            throw new NullPointerException("header cannot be null");
                        } else if (((String) v1).contains(":")) {
                            throw new IllegalArgumentException("header may not contain \':\'");
                        } else {
                            if (v0_4 == null) {
                                v0_5 = "";
                            }
                            v4.c.add(Pair.create(v1, v0_5));
                            continue;
                        }
                    }
                }

                if (!arg10) {
                    v0_5 = v2;
                } else if (!v2.endsWith(".apk")) {
                    v0_5 = v2 + ".apk";
                } else {
                    v0_5 = v2;
                }
                try {
                    label_70:
                    if ((arg10) || (v0_5.endsWith(".apk"))) {
                        arg12 = "application/vnd.android.package-archive";
                    }

                    if (!a.isTextEmpty(arg12)) {
                        v4.e = arg12;
                    }

                    v4.d = ((CharSequence) arg9);
                    File v1_1 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                    if (v1_1 != null && !v1_1.exists() && !v1_1.mkdirs()) {
                        return -1;
                    }

                    v1_1 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                    if (v1_1 == null) {
                        throw new IllegalStateException("Failed to get external storage public directory");
                    }

                    if (v1_1.exists()) {
                        if (!v1_1.isDirectory()) {
                            throw new IllegalStateException(v1_1.getAbsolutePath() + " already exists and is not a directory");
                        }
                    } else if (!v1_1.mkdirs()) {
                        throw new IllegalStateException("Unable to create directory: " + v1_1.getAbsolutePath());
                    }

                    if (v0_5 == null) {
                        throw new NullPointerException("subPath cannot be null");
                    }

                    v4.b = Uri.withAppendedPath(Uri.fromFile(v1_1), v0_5);
                    v4.i = arg15 ? 1 : 2;
                    v4.g = false;
                    if (arg16) {
                        v4.f = 2;
                    }

                    v0_1 = v3.a(v4);
                    if (!arg14) {
                        return v0_1;
                    }

                    UIUtils.showImageToast(arg11, R.drawable.doneicon_popup_textpage, R.string.toast_download_app);
                } catch (Throwable v0_2) {
                    label_39:
                    Logger.d("MyDownloadManager", "add download task error:" + v0_2);
                    v0_1 = -1;
                }
                return v0_1;
            }
        } catch (Exception v0) {
            v0_1 = -1;
            return v0_1;
        }
        return -1;
    }

    public String getDownloadExtra(Context arg3, long arg4) {
        Object v0_1;
        com.ss.android.download.g v0 = com.ss.android.download.g.a(arg3);
        if (v0.b != null) {
            v0_1 = v0.b.get(Long.valueOf(arg4));
        } else {
            v0_1 = null;
        }

        return ((String) v0_1);
    }

    public String getFailedReson(Context arg5, long arg6) {
        String v0_2;
        e.b v0 = new e.b();
        v0.a = new long[]{arg6};
        Cursor v1 = e.a(arg5).a(v0);
        int v0_1 = -1;
        if ((v1.moveToFirst()) && v1.getInt(v1.getColumnIndex("status")) == 16) {
            v0_1 = v1.getInt(v1.getColumnIndex("reason"));
        }

        switch (v0_1) {
            case 1000: {
                v0_2 = "ERROR_UNKNOWN";
                break;
            }
            case 1001: {
                v0_2 = "ERROR_FILE_ERROR";
                break;
            }
            case 1002: {
                v0_2 = "ERROR_UNHANDLED_HTTP_CODE";
                break;
            }
            case 1004: {
                v0_2 = "ERROR_HTTP_DATA_ERROR";
                break;
            }
            case 1005: {
                v0_2 = "ERROR_TOO_MANY_REDIRECTS";
                break;
            }
            case 1006: {
                v0_2 = "ERROR_INSUFFICIENT_SPACE";
                break;
            }
            case 1007: {
                v0_2 = "ERROR_DEVICE_NOT_FOUND";
                break;
            }
            case 1008: {
                v0_2 = "ERROR_CANNOT_RESUME";
                break;
            }
            case 1009: {
                v0_2 = "ERROR_FILE_ALREADY_EXISTS";
                break;
            }
            case 1010: {
                v0_2 = "ERROR_BLOCKED";
                break;
            }
            default: {
                v0_2 = String.valueOf(v0_1);
                break;
            }
        }

        return v0_2;
    }

    public void handleAppInstalled(final Context context, final String packageName) {
        com.bytedance.common.utility.b.a.a(new AsyncTask() {
            protected Object doInBackground(Object[] arg2) {
                return this.doInBackground(((Void[]) arg2));
            }

            protected Void doInBackground(Void[] arg9) {
                long v2;
                Void v7 = null;
                if (context == null) {
                    return v7;
                }

                if (a.isTextEmpty(packageName)) {
                    return v7;
                }

                e v0 = e.a(context);
                com.ss.android.download.e.b v1 = new com.ss.android.download.e.b();
                v1.c = Integer.valueOf(8);
                Cursor v1_1 = v0.a(v1);
                String v0_3;
                try {
                    do {
                        label_15:
                        if (!v1_1.moveToNext()) {
                            return v7;
                        }
                        v0_3 = v1_1.getString(v1_1.getColumnIndexOrThrow("local_filename"));
                        v2 = v1_1.getLong(v1_1.getColumnIndexOrThrow("_id"));
                        if (Logger.debug()) {
                            Logger.d("launcher_ad", "handleAppInstalled id = " + v2);
                        }

                        if (a.isTextEmpty(v0_3)) {
                            continue;
                        }
                        if (a.o() == null) {
                            continue;
                        }
                    } while (!a.o().a(context, v0_3, packageName));
                    a.v.a(v2, 4);
                    com.ss.android.download.g.a(context).a(context, v2);
                    return v7;
                } catch (Throwable v0_1) {
                } finally {
                    if (v1_1 != null) {
                        try {
                            v1_1.close();
                        } catch (Exception v1_2) {
                        }
                    }
                }
                return v7;
            }
        }, new Void[0]);
    }

    public void handleStatusClick(Context arg2, int arg3, long arg4, String arg6) {
        e.a(arg2, arg3, arg4, arg6);
    }

    public void hideNotification(Context arg3, long arg4) {
        com.ss.android.download.g.a(arg3).a(arg3, arg4);
    }

    public boolean isDownloadSuccessAndFileNotExist(Context arg7, DownloadShortInfo arg8) {
        boolean v0 = true;
        e v2 = e.a(arg7);
        if (arg8 == null || arg8.b != 8 || (e.b(arg8.fileName))) {
            v0 = false;
        } else {
            v2.a(new long[]{arg8.id});
        }

        return v0;
    }

    public DownloadShortInfo queryDownloadInfo(Context arg2, String arg3) {
        return e.a(arg2).a(arg3);
    }

    public void registerDownloadListener(Context arg5, Long arg6, DownloadInfoChangeListener arg7, String arg8, int arg9, String arg10) {
        com.ss.android.download.g v1 = com.ss.android.download.g.a(arg5);
        WeakHashMap v0_1 = (WeakHashMap) v1.a.get(arg6);
        if (v0_1 == null) {
            v0_1 = new WeakHashMap();
            v1.a.put(arg6, v0_1);
        }

        if (arg7 != null) {
            arg7.setDownloadId(arg6.longValue());
            v0_1.put(arg7, Boolean.TRUE);
            v1.c.put(arg6, new DownloadShortInfo());
        }

        if (!a.isTextEmpty(arg8)) {
            if (arg9 >= 0) {
                String v0_2 = arg8 + "##" + arg9;
                if (!a.isTextEmpty(arg10)) {
                    v0_2 = v0_2 + "##" + arg10;
                }

                v1.b.put(arg6, v0_2);
            } else {
                v1.b.put(arg6, arg8);
            }
        }
    }

    public void unregisterDownloadListener(Context arg4, Long arg5, DownloadInfoChangeListener arg6) {
        com.ss.android.download.g v1 = com.ss.android.download.g.a(arg4);
        Object v0 = v1.a.get(arg5);
        if (v0 != null) {
            ((WeakHashMap) v0).remove(arg6);
            v1.c.remove(arg5);
        }

        if (v0 == null || (((WeakHashMap) v0).isEmpty())) {
            v1.a.remove(arg5);
        }
    }
}

